//
//  HwwMakeOfficialSealSignatureView.m
//  HwwMakeOfficialSealSignature
//
//  Created by huangwenwu on 2020/5/6.
//

#import "HwwMakeOfficialSealSignatureView.h"
#import "HWWCircleTypeView.h"
#import <FuliBaseLibraryFramework/FuliBaseLibraryFramework.h>

@interface HwwMakeOfficialSealSignatureView()

@end
@implementation HwwMakeOfficialSealSignatureView

+(UIImage *)save:(UIView *)mybackgroundview save:(BOOL)isSave{
    UIGraphicsBeginImageContext(mybackgroundview.bounds.size); //currentView 当前的view
    [mybackgroundview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if (isSave) {
        UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);  //保存到相册中
    }
    return viewImage;
}

+ (RACSignal *)makeOfficialSeal:(NSString *)officialSealName save:(BOOL)isSave
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        //1,生成企业公章
        UIView *circleBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 170 *3, 170 *3)];
        circleBgView.layer.cornerRadius = 85 *3;
        circleBgView.layer.masksToBounds = YES;
        circleBgView.backgroundColor = [UIColor clearColor];
        UIImageView *circleViewImageView = [[UIImageView alloc]initWithImage:kGetImage(@"circleView")];
        circleViewImageView.frame = CGRectMake(0, 0, 170 *3, 170 *3);
        [circleBgView addSubview:circleViewImageView];
        HWWCircleTypeView *circleView = [[HWWCircleTypeView alloc]initWithFrame:CGRectMake(10 *3, 10 *3, 150 *3, 150 *3)];
        circleView.backgroundColor = [UIColor clearColor];
        circleView.text = officialSealName;
        circleView.textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:50], NSForegroundColorAttributeName:RGB16(0xE60012)};
        circleView.textAlignment = NSTextAlignmentCenter;
        circleView.verticalTextAlignment = HWWCircleTypeVerticalAlignOutside;
        circleView.baseAngle = 270 * M_PI / 180;
        circleView.characterSpacing = 1;
        circleView.layer.cornerRadius = 75 *3;
        circleView.layer.masksToBounds = YES;
        circleViewImageView.center = circleView.center;
        [circleBgView addSubview:circleView];
        
        [subscriber sendNext:[self save:circleBgView save:isSave]];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

+ (RACSignal *)makeSignature:(NSString *)officialSealName save:(BOOL)isSave
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        //2,生成签名公章
        UIView *signatureBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 68 *3 , 36 *3)];
        UIImageView *signatureImageView = [[UIImageView alloc]initWithImage:kGetImage(@"img_casingoutsideSignature")];
        signatureImageView.frame = CGRectMake(0, 0, 68 *3, 36 *3);
        [signatureBgView addSubview:signatureImageView];
        UILabel *signatureNameLabel = [[UILabel alloc]initWithFrame:signatureImageView.frame];
        signatureNameLabel.text = officialSealName;
        signatureNameLabel.font = [UIFont systemFontOfSize:50];
        signatureNameLabel.textColor = RGB16(0xE60012);
        signatureNameLabel.textAlignment = NSTextAlignmentCenter;
        [signatureBgView addSubview:signatureNameLabel];
        
        [subscriber sendNext:[self save:signatureBgView save:isSave]];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

@end
